package com.study.service.impl;


import com.study.bean.Blog;
import com.study.bean.BlogQuery;
import com.study.dao.IBlogDao;
import com.study.dao.impl.BlogDaoImpl;
import com.study.service.IBlogService;
import com.study.util.PageInfo;

import java.sql.SQLException;
import java.util.List;

public class BlogServiceImpl implements IBlogService {
    private IBlogDao blogDao = new BlogDaoImpl();

    @Override
    public List<Blog> selectAll() throws SQLException {
        return blogDao.selectAll();
    }
    
    

    /*@Override
    public LayUITableResult selectByPage(BlogQuery blogQuery) {
        int offset = (blogQuery.getPage() - 1) * blogQuery.getLimit();
        List<Blog> list = blogDao.selectByPage(offset, blogQuery.getLimit());
        //2.总的页数  查总的数量  Math.ceil(总数量/pageSize)
        int totalCount = blogDao.selectTotalCount();
        *//*LayUITableResult layUITableResult =new LayUITableResult();
        layUITableResult.setCode(0);
        layUITableResult.setCount(totalCount);
        layUITableResult.setData(list);*//*
        return LayUITableResult.ok(totalCount, list);
    }*/

    @Override
    public void deleteById(int id) throws SQLException {
        blogDao.deleteById(id);
    }

    @Override
    public void deleteAll(String[] ids) throws SQLException {
        //delete from blog where id in(2, 4, 5);
        for (String id : ids) {
            blogDao.deleteById(Integer.parseInt(id));
        }
    }

    @Override
    public void add(Blog blog) throws SQLException {
        blogDao.add(blog);
    }

    @Override
    public Blog selectById(int id) throws SQLException {
        return blogDao.selectById(id);
    }

    @Override
    public void update(Blog blog) throws SQLException {
        blogDao.update(blog);
    }

    @Override
    public PageInfo selectByPage(BlogQuery blogQuery) throws SQLException {
        List<Blog> blogs = blogDao.selectByPage(blogQuery);
        int count = blogDao.selectTotalCount(blogQuery);


        return PageInfo.ok(count, blogs);
    }
}
